/*
 * 创建日期 2005-10-9
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URI;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vanda.db.exception.MappingException;
import com.vanda.util.LogFunc;

/**
 * @author wkf5985
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class ReportUtils {
	private static Log log = LogFactory.getLog(ReportUtils.class);
	private static HashMap hmMethod = new HashMap();
	/**
	 * 取得dir+name的文件流
	 * @param name 文件名
	 * @param dir 文件目录
	 * @return
	 * @throws MappingException
	 */
	/*public static InputStream getMappingStream(String name, URI dir)
		throws ReportException {
		InputStream rsrc = null;
		//如果为空，则取系统路径
		if (dir == null) {
			LogFunc.info("Mapping resource: " + name);
			rsrc =
				Thread
					.currentThread()
					.getContextClassLoader()
					.getResourceAsStream(
					name);
			if (rsrc == null)
				rsrc =
			ReportUtils.class.getClassLoader().getResourceAsStream(
						name);
			if (rsrc == null)
				rsrc = ReportUtils.class.getResourceAsStream(name);
		} else {
			try {
				String path =
					(new File(new File(dir), name)).toURI().getRawPath();
				LogFunc.info("Mapping resource: " + path);
				rsrc = new FileInputStream(path);
			} catch (Exception e) {
				throw new ReportException(
					"Resource: " + name + " not found",
					e);
			}
		}
		if (rsrc == null)
			throw new ReportException("Resource: " + name + " not found");
		return rsrc;
	}*/
	
	public static InputStream getMappingStream(String name, URI dir)
		throws ReportException {
		InputStream rsrc = null;
		//如果为空，则取系统路径
		if (dir == null) {
			LogFunc.info("resource: " + name);
			rsrc =
				Thread
					.currentThread()
					.getContextClassLoader()
					.getResourceAsStream(
					name);
			if (rsrc == null)
				rsrc =
			ReportUtils.class.getClassLoader().getResourceAsStream(
						name);
			if (rsrc == null)
				rsrc = ReportUtils.class.getResourceAsStream(name);
		} else {
			try {
				/*String path =
					(new File(new File(dir), name)).toURI().getRawPath();
				LogFunc.info("Mapping resource: " + path);
				rsrc = new FileInputStream(path);*/
				rsrc = ReportUtils.class.getResourceAsStream(dir+"/"+name);
				LogFunc.info("resource: " + (dir+"/"+name));
			} catch (Exception e) {
				throw new ReportException(
					"Resource: " + name + " not found",
					e);
			}
		}
		if (rsrc == null)
			throw new ReportException("Resource: " + name + " not found");
		return rsrc;
	}
	
	/**
	 * 
	 * @param obj
	 * @param methodName
	 * @return
	 * @throws Exception
	 */
	private static Object invoke(Object obj,String methodName) throws Exception{
		String clsName = obj.getClass().getName();
		
		Method method = (Method)hmMethod.get(clsName);
		if(method == null){
			method = obj.getClass().getMethod(methodName,null);
		}
		return method.invoke(obj, null);
	}
	
	/**
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static Object clone(Object obj) throws ReportException{
		Object objClone = null;
		try{
			objClone = invoke(obj,"clone");
		}catch(Exception e){
			throw new ReportException(obj.getClass()+"is not support Clone!");
		}
		return objClone;
	}

	
	/**
	 * 取得数组实例的长度
	 * @param objs
	 * @return
	 */
	public static int getListSize(Object objs){
		if (objs instanceof List) {
			List listObjs = (List) objs;

			return listObjs.size();
		} else {
			Object[] objArray = (Object[]) objs;
			
			return objArray.length;
		}
	}
	
	public static Object get(Object objs,int i){
		if (objs instanceof List) {
			List listObjs = (List) objs;

			return listObjs.get(i);
		} else {
			Object[] objArray = (Object[]) objs;
			
			return objArray[i];
		}
	}
	
	public static BigDecimal convertBg(Object obj) {
		if (obj == null) {
			return null;
		}
		if (obj instanceof BigDecimal) {
			return (BigDecimal) obj;
		}

		return new BigDecimal(obj.toString());
	}

	public static Object convertObj(BigDecimal bg, Object obj) {
		if (bg == null) {
			return null;
		}

		if (obj instanceof BigDecimal) {
			return bg;
		} else if (obj instanceof Double) {
			return new Double(bg.doubleValue());
		} else if (obj instanceof String) {
			return bg.toString();
		} else if (obj instanceof Float) {
			return new Float(bg.floatValue());
		} else if (obj instanceof Long) {
			return new Long(bg.longValue());
		} else if (obj instanceof Integer) {
			return new Integer(bg.intValue());
		} else if (obj instanceof Short) {
			return new Integer(bg.shortValue());
		} else if (obj instanceof Byte) {
			return new Integer(bg.byteValue());
		}

		return null;
	}
}
